<?php

require_once '../config/config.php';
require_once 'Action.php';
require_once 'View.php';
require_once 'Library.php';

error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);
$_v = array();

foreach ($CAST_CONFIG['autoload'] as $lib) {
    Library::load($lib);
}
unset($lib);

$_v['root'] = trim($CAST_CONFIG['applicationRoot']);
if (strrpos($_v['root'], '/') == strlen($_v['root']) - 1) //if defined rot end with a slash, remove it
    $_v['root'] = substr_replace($_v['root'], '', strlen($_v['root']) - 1, 1);
$_v['root'] = trim($_v['root']);


$_v['full_url'] = $_SERVER['REQUEST_URI'];

if (empty($_v['root'])) //root is empty or is a single slash
    $_v['path'] = $_v['full_url'];
else
    $_v['path'] = (substr($_v['full_url'], strlen($_v['root']) + 1));

$_v['path'] = substr($_v['path'], 1);
$_v['pos'] = strpos($_v['path'], '?');

if (is_int($_v['pos']))
    $_v['path'] = substr($_v['path'], 0, $_v['pos']);

if (strrpos($_v['path'], '/') == strlen($_v['path']) - 1) //if defined rot end with a slash, remove it
    $_v['path'] = substr_replace($_v['path'], '', strlen($_v['path']) - 1, 1);

//echo "path is .$VARS['path'].";
//$VARS['path'] = trim($VARS['path']);

$CAST_PARAMS = explode('/', $_v['path']);
if (empty($_v['path'])) {
    $_v['target_action'] = 'index';
    $CAST_PARAMS = array();
}
else
    $_v['target_action'] = $CAST_PARAMS[0];


array_shift($CAST_PARAMS);
//print_r($CAST_PARAMS);

$CAST_BUNDLE = array();
$CAST_BUNDLE['view'] = $_v['target_action'];
$CAST_BUNDLE['dir'] = realpath('../');
$CAST_BUNDLE['layout'] = 'layout';
$CAST_BUNDLE['useLayout'] = true;
$CAST_BUNDLE['useView'] = true;
$CAST_BUNDLE['URI'] = $_v['path'];

$_v['title'] = $CAST_CONFIG['applicationName'];

////////////////Load bootstrap///////////////////
require_once '../config/bootstrap.php';
/////////////////////////////////////////////////


$_v['action_file'] = "../actions/{$_v['target_action']}.php";
if (file_exists($_v['action_file'])) {
    
    ob_start();

    require_once $_v['action_file'];
    $_v['buffer_content'] = ob_get_contents();

    //collect vars collected in action
    $vars = get_defined_vars();
    unset($vars['_v']);
    unset($vars['GLOBALS']);
    unset($vars['_POST']);
    unset($vars['_GET']);
    unset($vars['_SERVER']);
    unset($vars['_SESSION']);
    unset($vars['_FILES']);
    unset($vars['_COOKIE']);
    unset($vars['CAST_BUNDLE']);
    unset($vars['CAST_CONFIG']);
    unset($vars['CAST_PARAMS']);

    $_v['view_vars'] = $vars;
    
    ob_end_clean();
//    print_r($vars);
    unset($vars);
    $_v['content_found'] = true;
}

$_v['view_file'] = "../views/{$CAST_BUNDLE['view']}.{$CAST_CONFIG['viewExtension']}";

if ($CAST_BUNDLE['useView'] && file_exists($_v['view_file'])) {
    ob_start();
    require_once $_v['view_file'];
    $_v['buffer_content'] .= ob_get_contents();
    ob_end_clean();
    $_v['content_found'] = true;
} else if ($CAST_BUNDLE['useView']) {
    die('view not found');
}

if ($CAST_BUNDLE['useLayout']) {
    $_v['content'] = $_v['buffer_content'];
    $_v['layout'] = "../views/layouts/{$CAST_BUNDLE['layout']}.{$CAST_CONFIG['layoutExtension']}";
    require_once $_v['layout'];
} else
    echo $_v['buffer_content'];


if (!$_v['content_found']) {
    echo 'page not found';
}

function full($var) {
    return !empty ($var);
}

function blank($var) {
    return empty ($var);
}
?>